<?php
require_once "includes/common.php";
require_once ('includes/header.php');

$id = isset($_GET['id'])?$_GET['id']:0;
$sbmtBtn = "Add me !!";

//pr($_GET);
if(isset($_GET['mode']) && $_GET['mode']=='edit'){
  $selQuery = "SELECT * FROM `countries` WHERE `id`='$id'";
  $selResult = mysql_query($selQuery) or die(mysql_error());
  $ansCountry = mysql_fetch_assoc($selResult);
 // pr($ansCountry);
  $sbmtBtn = "Update me!!";

}

if(isset($_GET['mode']) && $_GET['mode']=='delete'){

  $delQuery = "DELETE FROM `countries` WHERE `id`='$id'";
  mysql_query($delQuery);
  $deletedRecords = mysql_affected_rows();
  echo "<h1 style='color:green'>$deletedRecords country deleted</h1>";
}
//$_POST['id']!=0 shows that we are UPDATING
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['id']!=0){
  $id = $_POST['id'];
  $name = $_POST['name'];
  $c_code = $_POST['c_code'];

  $updateQuery = "UPDATE `countries` SET `name`='$name',`c_code`='$c_code' WHERE `id`='$id' ";
  mysql_query($updateQuery) or die(mysql_error());
  if(mysql_affected_rows() > 0){
    echo "<h4>Country Updated</h4>";

    // set var to default to make sure addition
    $id = 0;$sbmtBtn = "Add me!!";$ansCountry['name'] = '';
  }
}


//$_POST['id']==0 shows that we are inserting
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['id']==0){
  $name = $_POST['name'];
  $c_code = $_POST['c_code'];
  $insert = "INSERT INTO `countries`
     (`name`,`c_code`)VALUES('$name','$c_code');";

mysql_query($insert) or die(mysql_error());

$rowsInserted =  mysql_affected_rows();
$lastId = mysql_insert_id();

echo "<p>$rowsInserted country added on Id:$lastId<p>";
}
//////////////////////////////////////////////////
$select = "SELECT * FROM `countries` ORDER BY `name`";

$result = mysql_query($select) or die(mysql_error());
$rowsSelected =  mysql_num_rows($result);
echo "<h4>$rowsSelected Countries Selected</h4>";
//////////////////////////////////////////////////
?>
<form action="" method="post" >
  <input type="hidden" name="id" value="<?=$id?>">
  <table width="40%" class="table table-striped">
    <thead>
      <tr>
        <th colspan="2">Add Country</th>

      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Name</td>
        <td><input type="text"
value="<?=@$ansCountry['name']?>"
         name="name"></td>
      </tr>
      <tr>
        <td>Country Code</td>
        <td><input type="text" value="<?=@$ansCountry['c_code']?>" name="c_code" maxlength="2"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="<?=$sbmtBtn?>" /></td>
      </tr>
    </tbody>
  </table>
</form>

  <table width="40%" class="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>Country</th>
      <th>C. Code</th>
      <th >Options</th>
    </tr>
  </thead>
  <tbody>
<?php
$i=1;
while($ans = mysql_fetch_assoc($result)){
?>
    <tr>
      <td><?=$i++?></td>
      <td><?=$ans['name']?></td>
      <td><?=$ans['c_code']?></td>
      <td>
<!-- Delete -->
<a href="country.php?id=<?=$ans['id']?>&mode=delete" onClick="return confirm('Are you sure ?')">Delete</a>
<!--Edit -->
<a href="country.php?id=<?=$ans['id']?>&mode=edit">Edit</a>

</td>
    </tr>
<?php
}//end while
?>
  </tbody>
</table>
